<!-- 污损耗材 -->
StainedGoods
<!-- 损耗管理 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="72px">
      <el-form-item label="耗材编号" prop="goods_id">
        <el-input v-model="queryParams.goods_id" placeholder="请输入耗材编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="耗材名称" prop="goods_name">
        <el-input v-model="queryParams.goods_name" placeholder="请输入耗材名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select clearable v-model="queryParams.status" placeholder="请选择">
          <el-option v-for="dict in orderStatusList" :key="dict.value" :value="dict.value" :label="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="操作人" prop="update_user_id">
        <InputAutocomplete
          v-model="iptVal"
          placeholder="请输入操作人"
          search_type="USER"
          @onSelect="(val) => (queryParams.update_user_id = val)"
        />
      </el-form-item>
      <el-form-item label="开始日期">
        <el-date-picker
          placeholder="请选择开始日期"
          v-model="queryParams.start_date"
          value-format="YYYY-MM-DD"
          type="date"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="结束日期">
        <el-date-picker
          placeholder="请选择结束日期"
          v-model="queryParams.end_date"
          value-format="YYYY-MM-DD"
          type="date"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="user_type === 3">
        <el-button type="primary" plain icon="Plus" @click="handleStainedAdd">损耗登记</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="goodsList" border>
      <el-table-column label="耗材编号" prop="goods_id" />
      <el-table-column label="耗材名称" prop="goods_name" min-width="120" show-overflow-tooltip />
      <el-table-column label="耗材规格" prop="goods_spec" min-width="100" show-overflow-tooltip />
      <el-table-column label="订单类型" prop="type_name" width="90" />
      <el-table-column label="订单状态" prop="status_name" width="80" />
      <el-table-column label="数量" prop="qty" />
      <el-table-column label="损耗日期" prop="order_date" width="160" />
      <el-table-column label="病历号" prop="medical_record_no" />
      <el-table-column label="病人姓名" prop="patient_name" />
      <el-table-column label="耗材批号" prop="batch_no" />
      <el-table-column label="耗材效期" prop="expiration_date" width="100" />
      <el-table-column label="备注" prop="remark" />
      <el-table-column label="操作人" prop="update_user_name" width="120" show-overflow-tooltip />
      <el-table-column label="操作日期" prop="update_date" width="160" />
      <el-table-column
        label="操作"
        v-if="user_type === 2"
        align="center"
        fixed="right"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <template v-if="scope.row.status === 0">
            <el-button link type="primary" @click="handleChangeConfirm(scope.row)">换货确认</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page_num"
      v-model:limit="queryParams.page_size"
      @pagination="getList"
    />

    <StainedDialog v-model="visible" @success="resetQuery" />
  </div>
</template>

<script setup name="Outbound">
import StainedDialog from "./stainedDialog.vue";

import useUserStore from "@/store/modules/user";

import { getOptionsList } from "@/api/common";
import { getStainedList } from "@/api/inventory/stainedGoods";
import { orderVerify } from "@/api/inventory/warehousing";

const { proxy } = getCurrentInstance();
const store = useUserStore();
const user_type = computed(() => store.user_type);

const showSearch = ref(true);
const loading = ref(false);
const queryParams = ref({
  page_num: 1,
  page_size: 10,
  goods_name: "",
  goods_id: "",
  type: "",
  start_date: "",
  end_date: ""
});
const goodsList = ref([]);
const total = ref(0);
const orderStatusList = ref([]);
const visible = ref(false);

const iptVal = ref("");

getList();

getOrderStatusList();

async function getOrderStatusList() {
  const res = await getOptionsList({ dict_type: "ORDER_STATUS" });
  if (res.code === 0) {
    orderStatusList.value = res.data;
  }
}

/** 查询列表 */
function getList() {
  loading.value = true;
  getStainedList(queryParams.value)
    .then((res) => {
      const data = res.data;
      goodsList.value = data.list;
      total.value = data.total;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page_num = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.update_user_id = "";
  queryParams.value.start_date = "";
  queryParams.value.end_date = "";
  iptVal.value = "";
  handleQuery();
}

// 损耗登记
const handleStainedAdd = () => {
  visible.value = true;
};
// 换货确认
const handleChangeConfirm = async (row) => {
  const res = await orderVerify({
    order_id: row.id,
    order_type: row.type
  });
  if (res && res.code === 0) {
    proxy.$modal.msgSuccess("换货确认成功");
    getList();
    return;
  }
  proxy.$modal.msgError(res.message);
};
</script>
